package com.example.notepad.timer

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.notepad.R

class StopwatchActivity : AppCompatActivity() {

    private lateinit var timerText: TextView
    private lateinit var btnStart: Button
    private lateinit var btnPause: Button
    private lateinit var btnContinue: Button
    private lateinit var btnReset: Button
    private lateinit var btnEnd: Button

    private var seconds = 0L // 记录经过的秒数
    private var countDownTimer: CountDownTimer? = null
    private var isRunning = false
    private var isPaused = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stopwatch)

        // 绑定控件
        timerText = findViewById(R.id.timerText)
        btnStart = findViewById(R.id.btnStart)
        btnPause = findViewById(R.id.btnPause)
        btnContinue = findViewById(R.id.btnContinue)
        btnReset = findViewById(R.id.btnReset)
        btnEnd = findViewById(R.id.btnEnd)

        // 设置按钮点击事件监听
        btnStart.setOnClickListener { startTimer() }
        btnPause.setOnClickListener { pauseTimer() }
        btnContinue.setOnClickListener { continueTimer() }
        btnReset.setOnClickListener { resetTimer() }
        btnEnd.setOnClickListener { finish() }

        // 初始化按钮状态
        updateButtonStates()
    }

    // 启动计时器
    private fun startTimer() {
        if (!isRunning) {
            isRunning = true
            isPaused = false
            countDownTimer = object : CountDownTimer(Long.MAX_VALUE, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    seconds++
                    updateTimerText()
                }

                override fun onFinish() {}
            }.start()

            // 更新按钮状态
            updateButtonStates()
        }
    }

    // 暂停计时器
    private fun pauseTimer() {
        if (isRunning) {
            isRunning = false
            isPaused = true
            countDownTimer?.cancel()

            // 更新按钮状态
            updateButtonStates()
        }
    }

    // 继续计时
    private fun continueTimer() {
        if (isPaused) {
            startTimer()

            // 更新按钮状态
            updateButtonStates()
        }
    }

    // 重置计时器
    private fun resetTimer() {
        seconds = 0
        updateTimerText()

        // 更新按钮状态
        updateButtonStates()
    }

    // 更新按钮的可用状态
    private fun updateButtonStates() {
        if (isRunning) {
            btnStart.isEnabled = false
            btnPause.isEnabled = true
            btnContinue.isEnabled = false
            btnReset.isEnabled = true
        } else if (isPaused) {
            btnStart.isEnabled = false
            btnPause.isEnabled = false
            btnContinue.isEnabled = true
            btnReset.isEnabled = true
        } else {
            btnStart.isEnabled = true
            btnPause.isEnabled = false
            btnContinue.isEnabled = false
            btnReset.isEnabled = false
        }
    }

    // 更新显示的时间
    private fun updateTimerText() {
        val minutes = seconds / 60
        val remainingSeconds = seconds % 60
        timerText.text = String.format("%02d:%02d:%02d", minutes / 60, minutes % 60, remainingSeconds)
    }
}
